package com.huatai.bpmn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.entity.BasicEntity;
import com.huatai.bpmn.entity.YYBasicEntity;
import com.huatai.bpmn.mapper.YYBasicMapper;
import com.huatai.bpmn.service.BasicDataService;
import com.huatai.bpmn.service.YYBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  表单表头配置
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@Service
public class YYBasicServiceImpl extends ServiceImpl<YYBasicMapper, YYBasicEntity> implements YYBasicService {

	@Autowired
	private BasicDataService basicDataService;
	/**
	 * 根据表单标识获取表单表头
	 * @param place
	 * @return
	 */
	@Override
	public List<YYBasicEntity> getColum(String place) {
		List<YYBasicEntity> list = this.list(new LambdaQueryWrapper<>(YYBasicEntity.class).like(YYBasicEntity::getPlace,place).eq(YYBasicEntity::getPid,"0").orderByAsc(YYBasicEntity::getSort));
		list.stream().sorted(Comparator.comparing(YYBasicEntity::getSort));
		list.stream().forEach(x->{
			String parentId = x.getId();
			List<YYBasicEntity> childrenList = this.list(new LambdaQueryWrapper<>(YYBasicEntity.class).like(YYBasicEntity::getPlace, place).eq(YYBasicEntity::getPid, parentId));
			x.setChildren(childrenList);
		});
		return list;
	}

	/**
	 * 获取表单所有表头
	 * @param place
	 * @return
	 */
	@Override
	public List<YYBasicEntity> getColumList(String place) {
		List<YYBasicEntity> list = this.list(new LambdaQueryWrapper<>(YYBasicEntity.class).like(YYBasicEntity::getPlace, place));

		//用于保存需要移除的数据
		ArrayList<YYBasicEntity> basicEntityList = new ArrayList<>();
		//因为place为x,x,x类型，使用like查询可能存在某些字段不属于place，过滤掉表头place不相同的表头
		list.stream().forEach(x->{
			if (x.getPlace().contains(",")){
				String[] array = x.getPlace().split(",");
				boolean b = Arrays.stream(array).anyMatch(y -> y.equals(place));
				if (!b){
					basicEntityList.add(x);
				}
			}else{
				if (!x.getPlace().equals(place)){
					basicEntityList.add(x);
				}
			}
		});
		list.removeAll(basicEntityList);
		return list;
	}
	@Override
	public YYBasicEntity getByJsonKey(String jsonKey) {
		return this.getOne(Wrappers.<YYBasicEntity>lambdaQuery().eq(YYBasicEntity::getJsonKey,jsonKey));
	}

	/**
	 * 获取excel表头
	 * @param place
	 * @return
	 */
	@Override
	public List<YYBasicEntity> getHead(String place) {
		/*List<String> list = this.list(new LambdaQueryWrapper<>(YYBasicEntity.class)
				.like(YYBasicEntity::getPlace, place)
				.eq(YYBasicEntity::getPid, "0")
				.orderByAsc(YYBasicEntity::getSort))
			.stream().map(x -> x.getJsonValue()).collect(Collectors.toList());*/
		List<YYBasicEntity> list = this.list(new LambdaQueryWrapper<>(YYBasicEntity.class)
				.like(YYBasicEntity::getPlace, place)
				.eq(YYBasicEntity::getPid, "0")
				.orderByAsc(YYBasicEntity::getSort))
			;

		return list;

	}

}
